import {Scene} from "phaser"

import {EVENTS_NAME, GameStatus, LEVELS} from '../../consts'
import {Score, ScoreOperations} from '../../classes/score'
import {Text} from '../../classes/text'
import {gameConfig} from "../../index";

export class UIScene extends Scene {
  private score!: Score
  private levelName!: string
  private gameEndPhrase!: Text
  public chestLootHandler: () => void
  public gameEndHandler: (status: GameStatus) => void

  constructor() {
    super('ui-scene')

    console.log('z00-触发吃掉奶酪事件');
    //吃掉怪物事件监听
    this.chestLootHandler = () => {
      console.log('z01-触发吃掉奶酪事件');

      this.score.changeValue(ScoreOperations.INCREASE, 10)

      const currentIndex = LEVELS.findIndex(
        (item) => item.name === this.levelName
      )

      if (LEVELS[currentIndex].score === this.score.getValue()) {
        const nextLevel = LEVELS[currentIndex + 1] //取下一关卡
        if (nextLevel) {
          this.game.events.off(EVENTS_NAME.chestLoot, this.chestLootHandler)
          this.game.events.off(EVENTS_NAME.gameEnd, this.gameEndHandler)
          this.scene.get("game-scene").scene.restart({name: nextLevel.name})
          this.scene.restart({name: nextLevel.name})
        } else {
          if (this.score.getValue() === gameConfig.winScore) {
            this.game.events.emit(EVENTS_NAME.gameEnd, "win") //发出游戏结束事件
          }
        }
      }
    }

    //游戏事件结束监听
    this.gameEndHandler = (status) => {
      this.cameras.main.setBackgroundColor("rgba(0,0,0,0.6)") //设置主相机的背景颜色，改变场景的背景色
      this.game.scene.pause('game-scene') //暂停该场景的更新和渲染，同时保持其状态（变量和对象）不变

      this.gameEndPhrase = new Text(
        this,
        this.game.scale.width / 2,
        this.game.scale.height * 0.4,
        status === GameStatus.LOSE
          ? `失败!\n\n点击屏幕重新开始`
          : `胜利!\n\n点击屏幕重新开始`
      )
        .setAlign('center')
        .setColor(status === GameStatus.LOSE ? '#ff0000' : '#ffffff')

      this.gameEndPhrase?.setPosition(
        this.game.scale.width / 2 - this.gameEndPhrase.width / 2,
        this.game.scale.height * 0.4
      )
      //this.gameEndPhrase.setPosition()方法用于设置文本对象的坐标位置，动态改变文本在场景的位置
      // pointerdown 是一个事件，用于检测用户在游戏对象上点击或按下鼠标按钮（或触摸屏幕）
      // this.game.scale.width : 游戏画布的宽度； this.gameEndPhrase.width： 文本对象的实际宽度
      this.input?.on("pointerdown", () => {
        console.log('z-触发-pointerdown')
        this.game.events.off(EVENTS_NAME.chestLoot, this.chestLootHandler) //this.game.events.off 移除游戏实例上的时间监听器
        this.game.events.off(EVENTS_NAME.gameEnd, this.gameEndHandler)
        this.scene.get("game-scene").scene.restart({name: "Level-1"})
        this.scene.restart({name: "Level-1"})
      })
    }
  }

  create(props: any): void {
    console.log('z-ui-scence-create', props)
    this.levelName = props.name
    this.score = new Score(this, 20, 20, 0)

    new Text(this, 20, 100, `z226关卡:${this.levelName}`)

    this.initListeners()
  }

  private initListeners(): void {
    console.log('z-开始奶酪事件监听', EVENTS_NAME.chestLoot)
    this.game.events.on(EVENTS_NAME.chestLoot, this.chestLootHandler, this);
    this.game.events.once(EVENTS_NAME.gameEnd, this.gameEndHandler, this);
  }
}
